package com.qinyunkj.searchchatserver.service.impl;

import com.qinyunkj.searchchatserver.config.CrawlerConfig;
import com.qinyunkj.searchchatserver.exception.CrawlerException;
import com.qinyunkj.searchchatserver.mapper.ArticleMapper;
import com.qinyunkj.searchchatserver.model.entity.Article;
import com.qinyunkj.searchchatserver.service.CrawlerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@RequiredArgsConstructor
public class CrawlerServiceImpl implements CrawlerService {

    private final CrawlerConfig crawlerConfig;
    private final ArticleMapper articleMapper;

    @Override
    public void crawl(String url) {
        try {
            log.info("开始爬取文章数据");
            for (int i = 0; i < 581; i++) {
                Document listDoc = getDocument("https://www.kantao.cn/labd/index_"+(i+1)+".html");
                List<String> articleUrls = parseArticleUrls(listDoc);

                List<Article> articles = new ArrayList<>();
                for (String articalUrl : articleUrls) {
                    try {
                        Article article = crawlArticleDetail(articalUrl);
                        if (article != null) {
                            articles.add(article);
                        }
                        TimeUnit.SECONDS.sleep(1);
                    } catch (Exception e) {
                        log.error("爬取文章详情失败: {}", url, e);
                    }
                }

                if (!articles.isEmpty()) {
                    for (Article article : articles){
                        try {
                            articleMapper.insert(article);
                        } catch (Exception e) {
                            log.info("保存文章失败", e);
                        }
                    }
                    log.info("成功爬取并保存 {} 篇文章", articles.size());
                }
            }

        } catch (Exception e) {
            log.error("爬虫任务执行失败", e);
            throw new CrawlerException("爬虫任务执行失败", e);
        }
    }

    private Document getDocument(String url) throws IOException {
        return Jsoup.connect(url)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
                .timeout(10000)
                .get();
    }

    private List<String> parseArticleUrls(Document doc) {
        List<String> urls = new ArrayList<>();
        // 根据实际网站结构调整选择器
        Elements elements = doc.select(".item a");
        for (Element element : elements) {
            if (element.hasClass("pic")){
                continue;
            }
            String href = element.attr("href");
            if (href.startsWith("/")) {
                href = crawlerConfig.getTargetUrl() + href;
            }
            urls.add(href);
        }
        return urls;
    }

    private Article crawlArticleDetail(String url) throws IOException {
        Document doc = getDocument(url);
        if (doc == null) {
            return null;
        }

        Article article = new Article();
        article.setUrl(url);
        // 根据实际网站结构调整选择器
        article.setTitle(doc.select("h1").text());
        article.setImage(doc.select(".article-content img").attr("src"));
        article.setContent(doc.select(".article-content").text());
        return article;
    }
}
